package com.intsig.airflow.web.exception;

import com.intsig.airflow.web.ajax.RestResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;

/**
 * @author leojie 2021/3/13 10:24 下午
 */
@Slf4j
public abstract class BaseExceptionHandler {
    protected RestResponseResult<String> serviceExceptionHandler(AirflowWebDesignException e) {
        log.error(e.getMessage());
        return RestResponseResult.failed(e.getMessage());
    }

    protected RestResponseResult<String> exceptionHandler(Exception e) {
        log.error(e.getMessage());
        e.printStackTrace();
        return RestResponseResult.failed("你把系统玩炸了，开发人员正在火速救场！");
    }

    protected RestResponseResult<String> validException(FieldError fieldError) {
        String errorMsg = "参数校验异常！";
        if (null != fieldError) {
            errorMsg = fieldError.getDefaultMessage();
            log.error("参数校验异常:{}({})", errorMsg, fieldError.getField());
        }
        return RestResponseResult.failed(errorMsg);
    }

    protected RestResponseResult<String> bindException(BindException e) {
        FieldError fieldError = e.getBindingResult().getFieldError();
        String errorMsg = "必填校验异常！";
        if (null != fieldError) {
            errorMsg = fieldError.getDefaultMessage();
            log.error("必填校验异常:{}({})", errorMsg, fieldError.getField());
        }
        return RestResponseResult.failed(errorMsg);
    }
}
